package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.CMDisinfectionConstant;
import com.csun.cmny.provider.model.domain.OmcCMDisinfectionTask;
import com.csun.cmny.provider.model.domain.OmcToiletDisinfectionRecord;
import com.csun.cmny.provider.model.dto.disinfection.BatchDisinfectionRecordDto;
import com.csun.cmny.provider.model.dto.disinfection.DisinfectionRecordDto;
import com.csun.cmny.provider.model.dto.disinfection.DisinfectionTaskDto;
import com.csun.cmny.provider.model.dto.disinfection.ToiletDisinfectionRecordDto;
import com.csun.cmny.provider.model.vo.disinfection.OmcDisinfectionRecordVo;
import com.csun.cmny.provider.model.vo.disinfection.OmcDisinfectionTaskVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcCMDisinfectionService;
import com.csun.cmny.provider.validator.OmcCMDisinfectionTaskValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/cmDisinfection",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcCMDisinfectionController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcCMDisinfectionService omcCMDisinfectionService;

    /**
     * 添加消毒项目
     */
    @ValidateAnnotation
    @PostMapping(value = "/addTask")
    public Wrapper addTask(@Validated(value = {OmcCMDisinfectionTaskValidator.class}) @RequestBody DisinfectionTaskDto disinfectionTaskDto, BindingResult br){
        if(disinfectionTaskDto.getTypeId().equals(CMDisinfectionConstant.TOILET_TYPE_ID)||disinfectionTaskDto.getTypeId().equals(CMDisinfectionConstant.CHAMBER_POT_TYPE_ID)){
            if(PublicUtil.isEmpty(disinfectionTaskDto.getOldmanId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("CMOldman.id.null",null),null);
            }
            if(PublicUtil.isEmpty(disinfectionTaskDto.getLocation())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("CMOldman.location.null",null),null);
            }
        }

        Integer result = omcCMDisinfectionService.addTask(disinfectionTaskDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改消毒项目
     */
    @ValidateAnnotation
    @PostMapping(value = "/updateTask")
    public Wrapper updateTask( @RequestBody DisinfectionTaskDto disinfectionTaskDto, BindingResult br){
        if(PublicUtil.isEmpty(disinfectionTaskDto.getId())){
            throw new OmcBizException(ErrorCodeEnum.OMC10032039,disinfectionTaskDto.getServiceItemNo());
        }
        Integer result = omcCMDisinfectionService.addTask(disinfectionTaskDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 删除消毒项目
     */
    @ValidateAnnotation
    @PostMapping(value = "/deleteTask/{serviceItemNo}")
    public Wrapper deleteTask(@PathVariable String serviceItemNo){

        Integer result = omcCMDisinfectionService.deleteTask(serviceItemNo);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 查询消毒项目
     */
    @ValidateAnnotation
    @PostMapping(value = "/selectTask")
    public Wrapper selectTask(@RequestBody DisinfectionTaskDto DisinfectionTaskDto){

        List<OmcDisinfectionTaskVo> omcCMDisinfectionTasks = omcCMDisinfectionService.selectTask(DisinfectionTaskDto);

        PageInfo<OmcDisinfectionTaskVo> pageInfo = new PageInfo<>(omcCMDisinfectionTasks);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 查询便器消毒项目
     */
    @ValidateAnnotation
    @PostMapping(value = "/selectToiletTask")
    public Wrapper selectToiletTask(@RequestBody DisinfectionTaskDto DisinfectionTaskDto){

        List<OmcCMDisinfectionTask> omcCMDisinfectionTasks = omcCMDisinfectionService.selectToileTask(DisinfectionTaskDto);

        PageInfo<OmcCMDisinfectionTask> pageInfo = new PageInfo<>(omcCMDisinfectionTasks);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加消毒记录
     */
    @ValidateAnnotation
    @PostMapping(value = "/add")
    public Wrapper add(@Validated @RequestBody BatchDisinfectionRecordDto batchDisinfectionRecordDto, BindingResult br){

        DisinfectionRecordDto disinfectionRecordDto = new DisinfectionRecordDto();
        BeanUtils.copyProperties(batchDisinfectionRecordDto,disinfectionRecordDto);
        Integer result=0;

        if(PublicUtil.isNotEmpty(batchDisinfectionRecordDto.getDisinfectionBindDtos())){
            //批量添加
            for(int i=0;i<batchDisinfectionRecordDto.getDisinfectionBindDtos().size();i++){
                BeanUtils.copyProperties(batchDisinfectionRecordDto.getDisinfectionBindDtos().get(i),disinfectionRecordDto);
                result+=omcCMDisinfectionService.add(disinfectionRecordDto, getLoginAuthDto());
            }
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /**
     * 查询消毒记录
     */
    @ValidateAnnotation
    @PostMapping(value = "/selectRecord")
    public Wrapper selectRecord(@RequestBody DisinfectionRecordDto disinfectionRecordDto){
        if(PublicUtil.isNotEmpty(disinfectionRecordDto.getEndQueryTime())){

            disinfectionRecordDto.setEndQueryTime(disinfectionRecordDto.getEndQueryTime()+" 23:59:59");
        }

        List<OmcDisinfectionRecordVo> list = omcCMDisinfectionService.selectRecord(disinfectionRecordDto);

        PageInfo<OmcDisinfectionRecordVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加便器消毒记录
     */
    @ValidateAnnotation
    @PostMapping(value = "/addToilet")
    public Wrapper addToilet(@Validated @RequestBody ToiletDisinfectionRecordDto toiletDisinfectionRecordDto, BindingResult br){

        Integer result = omcCMDisinfectionService.addToilet(toiletDisinfectionRecordDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /**
     * 查询便器消毒记录
     */
    @ValidateAnnotation
    @PostMapping(value = "/selectToiletRecord")
    public Wrapper selectToiletRecord(@RequestBody ToiletDisinfectionRecordDto toiletDisinfectionRecordDto){
        if(PublicUtil.isNotEmpty(toiletDisinfectionRecordDto.getEndQueryTime())){
            toiletDisinfectionRecordDto.setEndQueryTime(toiletDisinfectionRecordDto.getEndQueryTime()+" 23:59:59");
        }

        List<OmcToiletDisinfectionRecord> list = omcCMDisinfectionService.selectToiletRecord(toiletDisinfectionRecordDto);

        PageInfo<OmcToiletDisinfectionRecord> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
